// var i = (function () {
//     function e() {
//         this.curPercent = 0;
//         this.totalPercent = 0;
//         this.curIdx = 0;
//         this.loadDuration = 2;
//         this.loadList = [];
//         this.progressTween = null;
//     }
//     e.prototype.chushihua_renwu = function (e) {
//         this.onProgressChange = e.onProgressChange;
//         this.onLoadOver = e.onLoadOver;
//         if ("number" == typeof e.loadDuration) {
//             this.loadDuration = e.loadDuration;
//         }
//     };
//     e.prototype.tianjia_renwu = function (e) {
//         this.totalPercent += e.progress;
//         e.curPercent = this.totalPercent;
//         this.loadList.push(e);
//     };
//     e.prototype.kaishi_jiazai = function () {
//         var e = this;
//         this.loadList.forEach(function (t) {
//             t.curPercent = t.curPercent / e.totalPercent;
//         });
//         this.curIdx = 0;
//         this.curPercent = 0;
//         var t = 0;
//         !(function o() {
//             if (e.curIdx >= e.loadList.length) {
//                 if (++t >= 2 && e.onLoadOver) {
//                     e.onLoadOver();
//                 }
//             } else {
//                 {
//                     var i = e.loadList[e.curIdx];
//                     var n = i.curPercent;
//                     if (n > e.curPercent) {
//                         var a = e.loadDuration;
//                         if (e.progressTween) {
//                             e.progressTween.stop();
//                         }
//                         var s = Math.abs(n - e.curPercent) * a;
//                         e.progressTween = cc
//                             .tween(e)
//                             .to(
//                                 s,
//                                 {
//                                     curPercent: n
//                                 },
//                                 {
//                                     progress: function (t, o, i, n) {
//                                         i = (o - t) * n + t;
//                                         e.shuaxin_jindu(i);
//                                         return i;
//                                     }
//                                 }
//                             )
//                             .call(function () {
//                                 e.curPercent = n;
//                                 e.shuaxin_jindu(e.curPercent);
//                                 if (e.curPercent >= 1 && ++t >= 2 && e.onLoadOver) {
//                                     e.onLoadOver();
//                                 }
//                                 e.progressTween = null;
//                             })
//                             .start();
//                     }
//                     var r = i.loadAction;
//                     Log.ts(i.desc);
//                     r().then(function () {
//                         Log.te(i.desc);
//                         e.curIdx++;
//                         o();
//                     });
//                 }
//             }
//         })();
//     };
//     e.prototype.shuaxin_jindu = function (e) {
//         var t = "";
//         for (var o = 0; o < this.loadList.length; o++) {
//             if (e <= this.loadList[o].curPercent) {
//                 t = this.loadList[o].desc;
//                 break;
//             }
//         }
//         var i = {
//             progress: e,
//             desc: t
//         };
//         if (this.onProgressChange) {
//             this.onProgressChange(i);
//         }
//     };
//     return e;
// })();
// exports.default = i;


/**
 * 加载任务管理类
 */
class LoadTaskManager {
    constructor() {
        /** 当前进度百分比 */
        this.curPercent = 0;
        /** 总进度百分比 */
        this.totalPercent = 0;
        /** 当前加载索引 */
        this.curIdx = 0;
        /** 加载持续时间 */
        this.loadDuration = 2;
        /** 加载任务列表 */
        this.loadList = [];
        /** 进度动画 */
        this.progressTween = null;
    }

    /**
     * 初始化任务
     * @param {Object} config - 配置项
     */
    chushihua_renwu(config) {
        this.onProgressChange = config.onProgressChange;
        this.onLoadOver = config.onLoadOver;
        if (typeof config.loadDuration === 'number') {
            this.loadDuration = config.loadDuration;
        }
    }

    /**
     * 添加任务
     * @param {Object} task - 任务对象
     */
    tianjia_renwu(task) {
        this.totalPercent += task.progress;
        task.curPercent = this.totalPercent;
        this.loadList.push(task);
    }

    /**
     * 开始加载
     */
    kaishi_jiazai() {
        this.loadList.forEach(task => {
            task.curPercent = task.curPercent / this.totalPercent;
        });

        this.curIdx = 0;
        this.curPercent = 0;
        let retries = 0;

        const loadNext = () => {
            if (this.curIdx >= this.loadList.length) {
                if (++retries >= 2 && this.onLoadOver) {
                    this.onLoadOver();
                }
            } else {
                const task = this.loadList[this.curIdx];
                const nextPercent = task.curPercent;

                if (nextPercent > this.curPercent) {
                    const duration = Math.abs(nextPercent - this.curPercent) * this.loadDuration;
                    
                    if (this.progressTween) {
                        this.progressTween.stop();
                    }

                    this.progressTween = cc
                        .tween(this)
                        .to(
                            duration,
                            {
                                curPercent: nextPercent
                            },
                            {
                                progress: (startValue, endValue, currentTime, duration) => {
                                    const current_progress = (endValue - startValue) * duration + startValue;
                                    this.shuaxin_jindu(current_progress);
                                    return current_progress;
                                }
                            }
                        )
                        .call(() => {
                            this.curPercent = nextPercent;
                            this.shuaxin_jindu(this.curPercent);
                            if (this.curPercent >= 1 && ++retries >= 2 && this.onLoadOver) {
                                this.onLoadOver();
                            }
                            this.progressTween = null;
                        })
                        .start();
                }

                const loadAction = task.loadAction;
                Log.ts(task.desc);
                loadAction().then(() => {
                    Log.te(task.desc);
                    this.curIdx++;
                    loadNext();
                });
            }
        };

        loadNext();
    }

    /**
     * 刷新进度
     * @param {number} progress - 当前进度
     */
    shuaxin_jindu(progress) {
        let description = "";
        for (let index = 0; index < this.loadList.length; index++) {
            if (progress <= this.loadList[index].curPercent) {
                description = this.loadList[index].desc;
                break;
            }
        }

        const progressInfo = {
            progress: progress,
            desc: description
        };

        if (this.onProgressChange) {
            this.onProgressChange(progressInfo);
        }
    }
}

exports.default = LoadTaskManager;
